// JavaScript Document

(function($){
	$.fn.zPaging = function(options){
		console.log("Hi zPaging");
		
		//=============================================
		//Cac gia mac cua options
		//=============================================
		var defaults = {
			"rows"              : "#rows",
			"pages"				: "#pages",
			"items"				: 6,
			"height"			: 27,
			"currentPage"		: 1,
			"total"				: 0,
			"btnPrevious"		: ".goPrevious",
			"btnNext"			: ".goNext",
			"txtCurrentPage"	: "#currentPage",
			"pageInfo"			: ".pageInfo",
		};
		options = $.extend(defaults, options);
		
		//=============================================
		//Cac bien se su dung trong plugin
		//=============================================
		 var rows 			= $(options.rows)
		 var pages 			= $(options.pages);
		 var btnPrevious 	= $(options.btnPrevious);
		 var btnNext 		= $(options.btnNext);
		 var txtCurrentPage = $(options.txtCurrentPage);
		 var lblPageInfo	= $(options.pageInfo);
		 var aRows			= '';
		//=============================================
		//Khoi tao cac ham can thiet khi plugin su dung
		//=============================================
		init();
		 
		//=============================================
		//Ham khoi dong
		//=============================================
		function init() {
			//lay tong so trang
			$.ajax({
				url     : "../file/getdata.php?type=countttsach&items=" + options.items,
				type	: "GET",
				dataType: "json"
				
				}).done(function(data){
					
					options.total = data.total;
					console.log(options);
					pageInfo();
				    loadData(options.currentPage);

				});
			//gan cac su kien vao dieu khien
			setCurrentPage(options.currentPage);
			
			btnPrevious.on("click", function(e) {
				goPrevious();
				e.stopImmediatePropagation();
				});
				
			btnNext.on("click", function(e) {
			goNext();
			  });
				
			txtCurrentPage.on("keyup",function(e){
				
			if(e.keyCode == 13){
					var currentPageValue = parseInt($(this).val());
					console.log(currentPageValue);	
					if(isNaN(currentPageValue) || currentPageValue <= 0 
									|| currentPageValue > options.total){
						alert("Giá trị nhập không phù hợp, vui lòng xem lại!");
					}else{
						
						options.currentPage = currentPageValue;
						loadData(currentPageValue);
						pageInfo();
					}
				}
				
			});




		}
		
		//=============================================
		//Ham xu ly khi nhan vao btnPrevious
		//=============================================
		
		function goPrevious() {
			console.log("goPrevious: " + options.currentPage);
			if(options.currentPage != 1){
				var p = options.currentPage - 1;
				setCurrentPage(p);
				options.currentPage = p;
				pageInfo();
				loadData(p);	

			}
		}
		
		//=============================================
		//Ham xu ly khi nhan vao btnNext
		//=============================================
		
		function goNext() {
			console.log("goNext: " + options.currentPage);
			if(options.currentPage != options.total){
				var p = options.currentPage + 1;
				setCurrentPage(p);
				options.currentPage = p;
				pageInfo();
				loadData(p);	

			}
		}
		
		//=============================================
		//Gan gia tri vao txtbox
		//=============================================
		
		function setCurrentPage(value) {
			
			txtCurrentPage.val(value);
		}
		
		//=============================================
		//thong tin so trang
		//=============================================
		
		function pageInfo() {
			lblPageInfo.text("Page " + options.currentPage + " of " + options.total);			
		}
		
		
		//=============================================
		//Chieu cao cua paging
		//=============================================
		
		function setRowsHeight() {
			var ulHeight = (options.items * options.height) + "px";
			rows.css("height", ulHeight);
		}
		
		
		//=============================================
		//load cac thong tin trong database
		//=============================================
		
		function loadData(page) {
			
			console.log("loaddata");
			$.ajax({
				url: "../file/getdata.php?type=listttsach",
				type: "POST",
				dataType: "json",
				cache: false,
				data: {
					"items" 		: options.items,
					"currentPage" 	: options.currentPage,
				}
				}).done(function(data){
					//console.log(data);
					if(data.length >0)
					{
						rows.empty();
						$.each(data, function(i, val) {
							
							var str = '<tr><td width="50" class="center"><input class="checkbox1" type="checkbox" name="data[]" value="' + val.ID +'"></td>'
										+ ' <td width="150" class="center">' + val.ID + '</td>'
										+ ' <td width="650" class="center">' + val.Ten_tinh_trang + '</td>'
										+ ' <td width="150" class="">  <a href="suattsach.php?id=' +val.ID+'" target="_blank" > <button type="button" class="btn btn-primary" style="margin-right: 45px;; font-size:12px;"> Sửa</button></a></td> </tr>';
							rows.append(str);
							});
						}
					});
		}
		
		//=============================================
		//Xoa du lieu
		//=============================================
		
		function deleteItem(obj) {
		}
		
		
		
	}
})(jQuery);



$(document).ready(function(e) {

    var obj = {'items' : 5};
	$("#paging").zPaging(obj);
	
});


